import * as chitu from 'maishu-chitu';
// import BaseService from '../../share/service';
import { urlParams, serviceHost } from '../../share/common';
export { guid, imageUrl, parseUrlParams, serviceHost, urlParams } from '../../share/common';
import { Service as BaseService } from "maishu-services-sdk";

// export let tokens = (function () {

//     let appId = urlParams.appKey;
//     // let USER_TOKEN = `${appId}_userToken`;
//     // let userToken = new chitu.ValueStore(localStorage.getItem(USER_TOKEN));
//     let userToken = new chitu.ValueStore(Service.loginInfo.value ? Service.loginInfo.value.token : null);

//     userToken.add((value) => {
//         if (!value) {
//             // localStorage.removeItem(USER_TOKEN);
//             Service.loginInfo.value = null;
//             return;
//         }
//         // localStorage.setItem(USER_TOKEN, value);
//     })

//     return {
//         appId,
//         userToken
//     }

// })();





export let config = {
    pageSize: 10
}


let protocol = location.protocol;

let service_domain = serviceHost;

export abstract class Service extends BaseService {
    static error = chitu.Callbacks<Service, Error>();

    async ajax<T>(url: string, options: chitu.AjaxOptions) {

        let host = service_domain; //Ping.optimumServer ||
        url = `${protocol}//${host}/${url}`;

        options = options || {};
        options.headers = options.headers || {};

        let self = this;
        if (!this.applicationId) {
            let err = new Error("app token error");
            Service.error.fire(self, err);
            return;
        }

        options.headers['application-id'] = this.applicationId;
        if (Service.loginInfo.value)
            options.headers['token'] = Service.loginInfo.value.token;

        return super.ajax<T>(url, options);
    }

    static get storeName() {
        let key = `${urlParams.appKey}`;
        return "";
    }

    get protocol() {
        return location.protocol
    }

    get applicationId() {
        return urlParams.appKey;
    }
}

